#include <native_drawing/drawing_rect.h>
#include <native_window/external_window.h>
#include <native_drawing/drawing_bitmap.h>
#include <native_drawing/drawing_color.h>
#include <native_drawing/drawing_canvas.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_path.h>
#include <native_drawing/drawing_text_typography.h>
#include <native_drawing/drawing_text_blob.h>
#include <native_drawing/drawing_font.h>
#include <native_drawing/drawing_types.h>
#include <native_drawing/drawing_typeface.h>
#include <stdint.h>
#include <thread>
#include <cmath>
#include <iostream>
#include "native_node.h"
// #include "common/log_common.h"

OH_PixelmapNative* NativeNode::threadFunction(int count) {
    cBitmap = OH_Drawing_BitmapCreate();
    cCanvas = OH_Drawing_CanvasCreate();
    OH_Drawing_BitmapFormat format{COLOR_FORMAT_RGBA_8888, ALPHA_FORMAT_PREMUL};
    OH_Drawing_BitmapBuild(cBitmap, 500, 500, &format);
    OH_Drawing_CanvasBind(cCanvas, cBitmap);
    OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
    if (count % 2 == 0) {
        OH_Drawing_BrushSetColor(brush, 0xFFFF0000);
    } else {    
        OH_Drawing_BrushSetColor(brush, 0xFF0000FF);
    }
    OH_Drawing_Rect *rect = OH_Drawing_RectCreate(0, 0, 200, 200);
    OH_Drawing_CanvasAttachBrush(cCanvas, brush);
    OH_Drawing_CanvasDrawRect(cCanvas, rect);
    OH_Drawing_CanvasDetachBrush(cCanvas);
    // 创建pixmap
    OH_Pixelmap_InitializationOptions *createOps = nullptr;
    auto ret = OH_PixelmapInitializationOptions_Create(&createOps);
    if (ret != IMAGE_SUCCESS || !createOps) {
        OH_Drawing_BrushDestroy(brush);
        OH_Drawing_RectDestroy(rect);
        return nullptr;
    }

    OH_PixelmapInitializationOptions_SetWidth(createOps, 500);
    OH_PixelmapInitializationOptions_SetHeight(createOps, 500);
    OH_PixelmapInitializationOptions_SetPixelFormat(createOps, 3);    // 3 is RGBA fromat
    OH_PixelmapInitializationOptions_SetSrcPixelFormat(createOps, 3); // 3 is RGBA fromat
    OH_PixelmapInitializationOptions_SetAlphaType(createOps, 2);      // 2 is ALPHA_FORMAT_PREMUL

    size_t bufferSize = 500 * 500 * 4;
    void *bitmapAddr = OH_Drawing_BitmapGetPixels(cBitmap);
    OH_PixelmapNative *pixelMap = nullptr;
    ret = OH_PixelmapNative_CreatePixelmap((uint8_t *)bitmapAddr, bufferSize, createOps, &pixelMap);
    if (ret != IMAGE_SUCCESS || pixelMap == nullptr) {
        OH_Drawing_BrushDestroy(brush);
        OH_Drawing_RectDestroy(rect);
        OH_PixelmapInitializationOptions_Release(createOps);
        return nullptr;
    }
    OH_Drawing_BrushDestroy(brush);
    OH_Drawing_RectDestroy(rect);
    OH_PixelmapInitializationOptions_Release(createOps);
    return pixelMap;
}

void NativeNode::NativeOnDraw(OH_Drawing_Canvas * canvas, int32_t width, int32_t height) {
    OH_PixelmapNative *pixelMap = nullptr;
    OH_Drawing_Rect *srcRect = OH_Drawing_RectCreate(0, 0, 500, 500);
    OH_Drawing_Rect *dstRect = OH_Drawing_RectCreate(0, 0, 500, 500);
    OH_Drawing_SamplingOptions *samplingOptions = OH_Drawing_SamplingOptionsCreate(
        OH_Drawing_FilterMode::FILTER_MODE_NEAREST, OH_Drawing_MipmapMode::MIPMAP_MODE_NEAREST);
    for (int i = 0; i < 5; ++i) {
        std::thread thread([this, &pixelMap, &i]() { pixelMap = this->threadFunction(i); });
        thread.join();
        OH_Drawing_PixelMap *drPixelMap = OH_Drawing_PixelMapGetFromOhPixelMapNative(pixelMap);
        OH_Drawing_CanvasDrawPixelMapRect(canvas, drPixelMap, srcRect, dstRect, samplingOptions);
        OH_Drawing_PixelMapDissolve(drPixelMap);
        OH_Drawing_CanvasTranslate(canvas, 0, 500);
    }
    OH_PixelmapNative_Release(pixelMap);
    OH_Drawing_RectDestroy(srcRect);
    OH_Drawing_RectDestroy(dstRect);
    OH_Drawing_SamplingOptionsDestroy(samplingOptions);
    OH_Drawing_CanvasDestroy(cCanvas);
    OH_Drawing_BitmapDestroy(cBitmap);
}

NativeNode NativeNode::GetNodeById(int32_t id) {
    static NativeNode nativeNode;
    return nativeNode;
}